package com.neusoft.neuiotms.dcom.service.malfunction.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.converter.malfunction.MalfunctionSecondLeaderConverter;
import com.neusoft.neuiotms.dcom.dto.malfunction.MalfunctionSecondLeaderDTO;
import com.neusoft.neuiotms.dcom.model.malfunction.MalfunctionSecondLeader;
import com.neusoft.neuiotms.dcom.repository.malfunction.MalfunctionSecondLeaderRepository;
import com.neusoft.neuiotms.dcom.service.malfunction.MalfunctionSecondLeaderService;

@Service
public class MalfunctionSecondLeaderServiceImpl implements MalfunctionSecondLeaderService {

    @Autowired
    private MalfunctionSecondLeaderRepository secondLeaderRepository;
    @Autowired
    private MalfunctionSecondLeaderConverter secondLeaderConverter;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @Transactional
    @Override
    public MalfunctionSecondLeaderDTO create(final MalfunctionSecondLeaderDTO dto, final UserBean user) {
        // 设置当前节点操作人
        dto.setOperatorCode(user.getUsername());
        dto.setOperatorName(user.getName());

        // 设置二线人员的审批人编码
        this.processInstanceService.setGlobalVariable(dto.getTaskId(),
                DCOMConstants.MALFUNCTION_PROCESS_EXECUTOR_SECOND_OPERATOR_CODE,
                dto.getSecondOperatorCode());
        // 设置二线的审批人名称
        this.processInstanceService.setGlobalVariable(dto.getTaskId(),
                DCOMConstants.MALFUNCTION_PROCESS_EXECUTOR_SECOND_OPERATOR_NAME,
                dto.getSecondOperatorName());

        // 完成节点
        this.processInstanceService.complete(dto.getWorkNum(), dto.getTaskId(), user);

        // 查询当前workNum和TaskId下是否存在记录
        final MalfunctionSecondLeader oldModel = this.secondLeaderRepository.findByWorkNumAndTaskId(
                dto.getWorkNum(), dto.getTaskId());

        // 如果不存在新增
        if (oldModel == null) {
            dto.setId(null);
        }

        // 保存操作信息
        final MalfunctionSecondLeader model = this.secondLeaderConverter.toModel(dto);
        this.secondLeaderRepository.save(model);
        return this.secondLeaderConverter.toDTO(model);
    }

    @Override
    public MalfunctionSecondLeaderDTO detail(final String workNum, final String taskId, final UserBean user) {
        MalfunctionSecondLeader model = null;

        try {
            if (StringUtils.isNotBlank(workNum) && !DCOMConstants.STR_NULL.equals(taskId)) {
                model = this.secondLeaderRepository.findByWorkNumAndTaskId(workNum, taskId);
                if (model == null) {

                    // 获取前一节点的TaskID
                    final String fromProcessTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (fromProcessTaskId == null) {
                        final MalfunctionSecondLeaderDTO dto = new MalfunctionSecondLeaderDTO();
                        return dto;
                    } else {
                        model = this.secondLeaderRepository.findByWorkNumAndTaskId(workNum, fromProcessTaskId);
                    }
                }
            } else if (DCOMConstants.STR_NULL.equals(taskId)) {
                model = this.secondLeaderRepository.findTopByWorkNumOrderByIdDesc(workNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workNum },
                    new Object[] {taskId });
        }

        // 设置是否解决流程变量
        final MalfunctionSecondLeaderDTO dto = this.secondLeaderConverter.toDTO(model);
        return dto;
    }

}
